package com.ruoyi.servie.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayOpenAppMiniTemplatemessageSendModel;
import com.alipay.api.request.AlipayOpenAppMiniTemplatemessageSendRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipayOpenAppMiniTemplatemessageSendResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.ruoyi.servie.param.AliAppleTemplateMsgParams;
import com.ruoyi.servie.service.IAliAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AliAppletServiceImpl implements IAliAppletService {

    @Autowired
    private AlipayClient alipayClient;

    @Override
    public AlipaySystemOauthTokenResponse getSystemOauthToken(String code) {

        AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
        alipaySystemOauthTokenRequest.setGrantType("authorization_code");
        alipaySystemOauthTokenRequest.setCode(code);
        AlipaySystemOauthTokenResponse response = null;
        try{
            response = alipayClient.execute(alipaySystemOauthTokenRequest);
            //下面时获取用户其他信息
            //String accessToken = alipaySystemOauthTokenResponse.getAccessToken();
            //AlipayUserUserinfoShareRequest alipayUserUserinfoShareRequest = new AlipayUserUserinfoShareRequest();
            //AlipayUserUserinfoShareResponse alipayUserUserinfoShareResponse = alipayClient.execute(alipayUserUserinfoShareRequest, accessToken);

        }catch (Exception e){
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public AlipayOpenAppMiniTemplatemessageSendResponse sendTemplateMsg(AliAppleTemplateMsgParams templateMsg) throws AlipayApiException {

        AlipayOpenAppMiniTemplatemessageSendRequest request =new AlipayOpenAppMiniTemplatemessageSendRequest();
        AlipayOpenAppMiniTemplatemessageSendModel model = new AlipayOpenAppMiniTemplatemessageSendModel();
        model.setToUserId(templateMsg.getTo_user_id());
        model.setFormId(templateMsg.getForm_id());
        model.setUserTemplateId(templateMsg.getUser_template_id());
        model.setPage(templateMsg.getPage());
        List<String> templateData = templateMsg.getTemplateData();
        Map<String, Map<String,String>> data = new HashMap<>();
        Map<String,String> mapv;
        if (templateData != null) {
            for (int i = 0; i < templateData.size(); i++) {
                int key = i + 1;
                String name = "keyword" + key;
                mapv = new HashMap<>();
                mapv.put("value", templateData.get(i));
                data.put(name, mapv);
            }
        }
        model.setData(JSON.toJSONString(data));
        request.setBizContent(JSONObject.toJSONString(model));
        AlipayOpenAppMiniTemplatemessageSendResponse response = alipayClient.execute(request);
        return response;
    }
}
